package com.yehuishou.controller;

import static org.hamcrest.CoreMatchers.instanceOf;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FilenameUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yehuishou.dao.FileDao;
import com.yehuishou.pojo.AssessmentValue;
import com.yehuishou.pojo.BackAssessment;
import com.yehuishou.pojo.BackItem;
import com.yehuishou.pojo.Brand;
import com.yehuishou.pojo.ImageFile;
import com.yehuishou.pojo.Models;
import com.yehuishou.service.AssessmentValueService;
import com.yehuishou.service.BackAssessmentService;
import com.yehuishou.service.BackItemService;
import com.yehuishou.service.BrandService;
import com.yehuishou.service.ClassifyService;
import com.yehuishou.service.ModelService;
/**
 * 
 * @author xiaosa
 * @className BackItemController.java
 * @descripe 
 * @data 2018年5月23日
 */

@Controller
public class BackItemController {

	String rootPath = "/upload/";

	@Autowired
	FileDao fileDao;

	Map<String, Object> fileIdMap = new HashMap<>();

	@Autowired

	private BackItemService backItemService;

	@Autowired

	private BrandService brandService;

	@Autowired

	private ClassifyService classifyService;

	@Autowired

	private ModelService modelService;
	
	@Autowired
	private BackAssessmentService backAssessmentService;
	
	@Autowired
	private AssessmentValueService assessmentValueService;

	@RequestMapping("/content-1_1")
	public String selectBackItem(HttpServletRequest req, @Param(value = "npage") String npage, // 当前页码
			@Param(value = "npagesize") String npagesize, //
			@Param(value = "bname") String bname, // 商品名称
			@Param(value = "classify") String classify, // 商品分类id
			@Param(value = "brand") String brand// 商品品牌id

	) {

		// 创建backitem对象 用于存放参数属性
		int classifys = 0;
		int brands = 0;
        System.out.println("---商品的型号--："+bname);
		BackItem searchitem = new BackItem();

		if ((classify != null) && (classify != "")) {

			classifys = Integer.parseInt(classify);
			searchitem.setCid(classifys);
		
		}

		if ((brand != null) && (brand != "")) {

			brands = Integer.parseInt(brand);
			searchitem.setPid(brands);
		}

		if ((bname != null) && (bname != "")) {

			searchitem.setModel(bname);
		}

		int page = 0;
		int pagesize = 0;

		if ((npage == null) || (npage == "")) {

			page = 1;

		} else {

			page = Integer.parseInt(npage);

		}

		if ((npagesize == null) || (npagesize == "")) {
			// 默认每页为5个记录
			pagesize = 10;

		} else {
			// 前台指定页面记录个数
			pagesize = Integer.parseInt(npagesize);

		}

		PageHelper.startPage(page, pagesize);
		// 将查询到的数据放入pageinfo中
		PageInfo pageinfo = new PageInfo<BackItem>(backItemService.getBack(searchitem));

		// 保存所有的分类列表
		req.setAttribute("classifylist", classifyService.getAllClassify());

		if ((classify == null) || (classify == "")) {
			req.setAttribute("brandlist", brandService.getAllBrand(classifyService.getAllClassify().get(1).getCid()));
		}

		else

		{
			req.setAttribute("brandlist", brandService.getAllBrand(classifys));

		}
		// 分页插件数据
		req.setAttribute("pageinfo", pageinfo);

		// 保存搜索的参数
		req.setAttribute("searchitem", searchitem);

		return "content-1_1";

	}
     //删除一条商品
	@ResponseBody
	@RequestMapping("/deleteBackItem")
	public boolean deleteBackItem(@Param(value = "id") int id) {
		backItemService.deleteById(id);
		return true;
	}

	// 跳到添加回收商品页面
	@RequestMapping("/addBackitem")
	public String addBackItem(HttpServletRequest req) {
		System.out.println("-----跳转商品页面---");

		req.setAttribute("classifylist", classifyService.getAllClassify());

		List<Brand> brandlist = brandService.getAllBrand(classifyService.getAllClassify().get(0).getCid());
		// 再获取集合中第一个classify 对象的id

		req.setAttribute("brandlist", brandlist);

		req.setAttribute("modellist", modelService.getModelByid(brandlist.get(0).getPid()));
		req.setAttribute("as", backAssessmentService.getBackAssessment(classifyService.getAllClassify().get(0).getCid()));
		
		
		return "backitem_add";
	}
    
	
	//添加商品
	@RequestMapping("/addBackItem_s")
	public String addBackItems(BackItem backitem,
		String[] assess0,
		String[] assess1,
		String[] assess2,
		String[] assess3,
		String[] assess4,
		String[] assess5,
		String[] assess6,
		String[] assess7,
		String[] assess8,
		String[] assess9,
		String[] assess10,
		String[] assess11,
		String[] assess12,
		String[] assess13,
		String[] assess14,
		String[] assess15,
		String[] assess16,
		String[] assess17,
		String[] assess18,
		String[] assess19,
		String[] assess20,
		String[] money0,
		String[] money1,
		String[] money2,
		String[] money3,
		String[] money4,
		String[] money5,
		String[] money6,
		String[] money7,
		String[] money8,
		String[] money9,
		String[] money10,
		String[] money11,
		String[] money12,
		String[] money13,
		String[] money14,
		String[] money15,
		String[] money16,
		String[] money17,
		String[] money18,
		String[] money19,
		String[] money20
			) {
		 Map<String, Object> map  = new HashMap<>();
		backitem.setBacktime(new Date());
		// 执行添加操作
		backItemService.addBackItem(backitem);
	 Integer back_id =backitem.getId();
	//获取该cid对应的属性集合
	 List<BackAssessment> list = backAssessmentService.getBackAssessment(backitem.getCid());
	   AssessmentValue assessmentValue  = new AssessmentValue();
	   System.out.println("----------------------"+back_id);
	   assessmentValue.setBack_id(back_id);
	   if(list.size()>0) {
     if(assess0!=null&&assess0.length>0) {
    	 for(int i=0;i<assess0.length;i++) {
    		
    		 assessmentValue.setAssess_id(list.get(0).getAssess_id());
    		 assessmentValue.setAssess_value(assess0[i]);
    		 assessmentValue.setAssess_price(Double.parseDouble(money0[i]));
    		 assessmentValueService.insertAssessmentValue(assessmentValue);
    	 }	 
     } 
    }
	   
	   if(list.size()>1) {
     if(assess1!=null&&assess1.length>0) {
    	 for(int i=0;i<assess1.length;i++) {
    		
    		 assessmentValue.setAssess_id(list.get(1).getAssess_id());
    		 assessmentValue.setAssess_value(assess1[i]);
    		 assessmentValue.setAssess_price(Double.parseDouble(money1[i]));
    		 assessmentValueService.insertAssessmentValue(assessmentValue);
    	 }	 
     }
	   }
	   
	   if(list.size()>2) {
     if(assess2!=null&&assess2.length>0) {
    	 for(int i=0;i<assess2.length;i++) {
    		
    		 assessmentValue.setAssess_id(list.get(2).getAssess_id());
    		 assessmentValue.setAssess_value(assess2[i]);
    		 assessmentValue.setAssess_price(Double.parseDouble(money2[i]));
    		 assessmentValueService.insertAssessmentValue(assessmentValue);
    	 }	 
     }
	   }
	   if(list.size()>3) {
     if(assess3!=null&&assess3.length>0) {
    	 for(int i=0;i<assess3.length;i++) {
    		
    		 assessmentValue.setAssess_id(list.get(3).getAssess_id());
    		 assessmentValue.setAssess_value(assess3[i]);
    		 assessmentValue.setAssess_price(Double.parseDouble(money3[i]));
    		 assessmentValueService.insertAssessmentValue(assessmentValue);
    	 }	 
     }
	   }
	   if(list.size()>4) {
     if(assess4!=null&&assess4.length>0) {
    	 for(int i=0;i<assess4.length;i++) {
    		
    		 assessmentValue.setAssess_id(list.get(4).getAssess_id());
    		 assessmentValue.setAssess_value(assess4[i]);
    		 assessmentValue.setAssess_price(Double.parseDouble(money4[i]));
    		 assessmentValueService.insertAssessmentValue(assessmentValue);
    	 }	 
     }
	   }
	   if(list.size()>5) {
     if(assess5!=null&&assess5.length>0) {
    	 for(int i=0;i<assess5.length;i++) {
    		
    		 assessmentValue.setAssess_id(list.get(5).getAssess_id());
    		 assessmentValue.setAssess_value(assess5[i]);
    		 assessmentValue.setAssess_price(Double.parseDouble(money5[i]));
    		 assessmentValueService.insertAssessmentValue(assessmentValue);
    	 }	 
     }
	   }
	   if(list.size()>6) {
     if(assess6!=null&&assess6.length>0) {
    	 for(int i=0;i<assess6.length;i++) {
    		
    		 assessmentValue.setAssess_id(list.get(6).getAssess_id());
    		 assessmentValue.setAssess_value(assess6[i]);
    		 assessmentValue.setAssess_price(Double.parseDouble(money6[i]));
    		 assessmentValueService.insertAssessmentValue(assessmentValue);
    	 }	 
     }
	   }
	   if(list.size()>7) {
     if(assess7!=null&&assess7.length>0) {
    	 for(int i=0;i<assess7.length;i++) {
    		
    		 assessmentValue.setAssess_id(list.get(7).getAssess_id());
    		 assessmentValue.setAssess_value(assess7[i]);
    		 assessmentValue.setAssess_price(Double.parseDouble(money7[i]));
    		 assessmentValueService.insertAssessmentValue(assessmentValue);
    	 }	 
     }
	   }
	   if(list.size()>8) {
     if(assess8!=null&&assess8.length>0) {
    	 for(int i=0;i<assess8.length;i++) {
    		 assessmentValue.setAssess_id(list.get(8).getAssess_id());
    		 assessmentValue.setAssess_value(assess8[i]);
    		 assessmentValue.setAssess_price(Double.parseDouble(money8[i]));
    		 assessmentValueService.insertAssessmentValue(assessmentValue);
    	 }	 
     }
	   }
	   if(list.size()>9) {
     if(assess9!=null&&assess9.length>0) {
    	 for(int i=0;i<assess9.length;i++) {
    		 assessmentValue.setAssess_id(list.get(9).getAssess_id());
    		 assessmentValue.setAssess_value(assess9[i]);
    		 assessmentValue.setAssess_price(Double.parseDouble(money9[i]));
    		 assessmentValueService.insertAssessmentValue(assessmentValue);
    	 }	 
     }
	   }
	   if(list.size()>10) {
     if(assess10!=null&&assess10.length>0) {
    	 for(int i=0;i<assess10.length;i++) {
    		 assessmentValue.setAssess_id(list.get(10).getAssess_id());
    		 assessmentValue.setAssess_value(assess10[i]);
    		 assessmentValue.setAssess_price(Double.parseDouble(money10[i]));
    		 assessmentValueService.insertAssessmentValue(assessmentValue);
    	 }	 
     }
	   }
	   if(list.size()>11) {
		     if(assess11!=null&&assess11.length>0) {
		    	 for(int i=0;i<assess11.length;i++) {
		    		 assessmentValue.setAssess_id(list.get(11).getAssess_id());
		    		 assessmentValue.setAssess_value(assess11[i]);
		    		 assessmentValue.setAssess_price(Double.parseDouble(money11[i]));
		    		 assessmentValueService.insertAssessmentValue(assessmentValue);
		    	 }	 
		     }
			   }
	   if(list.size()>12) {
		     if(assess12!=null&&assess12.length>0) {
		    	 for(int i=0;i<assess12.length;i++) {
		    		 assessmentValue.setAssess_id(list.get(12).getAssess_id());
		    		 assessmentValue.setAssess_value(assess12[i]);
		    		 assessmentValue.setAssess_price(Double.parseDouble(money12[i]));
		    		 assessmentValueService.insertAssessmentValue(assessmentValue);
		    	 }	 
		     }
			   }
	   if(list.size()>13) {
		     if(assess13!=null&&assess13.length>0) {
		    	 for(int i=0;i<assess13.length;i++) {
		    		 assessmentValue.setAssess_id(list.get(13).getAssess_id());
		    		 assessmentValue.setAssess_value(assess13[i]);
		    		 assessmentValue.setAssess_price(Double.parseDouble(money13[i]));
		    		 assessmentValueService.insertAssessmentValue(assessmentValue);
		    	 }	 
		     }
			   }
	   if(list.size()>14) {
		     if(assess14!=null&&assess14.length>0) {
		    	 for(int i=0;i<assess14.length;i++) {
		    		 assessmentValue.setAssess_id(list.get(14).getAssess_id());
		    		 assessmentValue.setAssess_value(assess14[i]);
		    		 assessmentValue.setAssess_price(Double.parseDouble(money14[i]));
		    		 assessmentValueService.insertAssessmentValue(assessmentValue);
		    	 }	 
		     }
			   }
	   if(list.size()>15) {
		     if(assess15!=null&&assess15.length>0) {
		    	 for(int i=0;i<assess15.length;i++) {
		    		 assessmentValue.setAssess_id(list.get(15).getAssess_id());
		    		 assessmentValue.setAssess_value(assess15[i]);
		    		 assessmentValue.setAssess_price(Double.parseDouble(money15[i]));
		    		 assessmentValueService.insertAssessmentValue(assessmentValue);
		    	 }	 
		     }
			   }
	   if(list.size()>16) {
		     if(assess16!=null&&assess16.length>0) {
		    	 for(int i=0;i<assess16.length;i++) {
		    		 assessmentValue.setAssess_id(list.get(16).getAssess_id());
		    		 assessmentValue.setAssess_value(assess16[i]);
		    		 assessmentValue.setAssess_price(Double.parseDouble(money16[i]));
		    		 assessmentValueService.insertAssessmentValue(assessmentValue);
		    	 }	 
		     }
			   }
	   if(list.size()>17) {
		     if(assess17!=null&&assess17.length>0) {
		    	 for(int i=0;i<assess17.length;i++) {
		    		 assessmentValue.setAssess_id(list.get(17).getAssess_id());
		    		 assessmentValue.setAssess_value(assess17[i]);
		    		 assessmentValue.setAssess_price(Double.parseDouble(money17[i]));
		    		 assessmentValueService.insertAssessmentValue(assessmentValue);
		    	 }	 
		     }
			   }
	   if(list.size()>18) {
		     if(assess18!=null&&assess18.length>0) {
		    	 for(int i=0;i<assess18.length;i++) {
		    		 assessmentValue.setAssess_id(list.get(18).getAssess_id());
		    		 assessmentValue.setAssess_value(assess18[i]);
		    		 assessmentValue.setAssess_price(Double.parseDouble(money18[i]));
		    		 assessmentValueService.insertAssessmentValue(assessmentValue);
		    	 }	 
		     }
			   }
	   if(list.size()>19) {
		     if(assess19!=null&&assess19.length>0) {
		    	 for(int i=0;i<assess19.length;i++) {
		    		 assessmentValue.setAssess_id(list.get(19).getAssess_id());
		    		 assessmentValue.setAssess_value(assess19[i]);
		    		 assessmentValue.setAssess_price(Double.parseDouble(money19[i]));
		    		 assessmentValueService.insertAssessmentValue(assessmentValue);
		    	 }	 
		     }
			   }
	   if(list.size()>20) {
		     if(assess20!=null&&assess20.length>0) {
		    	 for(int i=0;i<assess20.length;i++) {
		    		 assessmentValue.setAssess_id(list.get(20).getAssess_id());
		    		 assessmentValue.setAssess_value(assess20[i]);
		    		 assessmentValue.setAssess_price(Double.parseDouble(money20[i]));
		    		 assessmentValueService.insertAssessmentValue(assessmentValue);
		    	 }	 
		     }
			   }
	 return "redirect:content-1_1";
	}

	// 批量删除的ajax
	@ResponseBody
	@RequestMapping("/deleteBackItems")
	public boolean deleteBackItems(@RequestParam(value = "td_s[]") Integer[] td_s) {
		
		//定义需要删除的属性值的集合
		List<Integer> ids = new ArrayList<>();
		for(int k=0;k<td_s.length;k++) {
		
		AssessmentValue as = new AssessmentValue();
		//设置要要删除属性的评估商品id
		as.setBack_id(td_s[k]);
		//获取竞价商品对应的商品属性值集合
		List<AssessmentValue> ass =assessmentValueService.getAssessmentValue(as);
		for(int i=0;i<ass.size();i++) {
			Integer assess_id =ass.get(i).getVal_id();
			//获取属性集合对象的主键id
			ids.add(assess_id);
		 }
		}
		//删除评估属性值表
		assessmentValueService.deleteAssessByIds(ids);
		
		//删除竞价商品
	   backItemService.deleteByIds(td_s);
		return true;
		
}  
	
	

	// 获取品牌的菜单的ajax请求
	@ResponseBody
	@RequestMapping("/getbrandByid")
	public Map<String, Object> getBrandByid(int cid) {

		Map<String, Object> map = new HashMap<>();

		List<Brand> brands = brandService.getAllBrand(cid);

		List<Models> models = modelService.getModelByid(brands.get(0).getPid());

		map.put("brands", brands);
		map.put("models", models);
		map.put("assessment", backAssessmentService.getBackAssessment(cid));
		return map;

	}

	// 获取型号的下拉菜单的ajax
	@ResponseBody
	@RequestMapping("/getModelByid")
	public List<Models> getModelByid(int pid) {
		return modelService.getModelByid(pid);

	}

	// 图片文件上传
	@ResponseBody
	@RequestMapping("/updaLoadImage")
	public Map<String, Object> uploadfile(@RequestParam(name = "modelPath", required = false) String modelPath,
			@RequestParam(name = "file", required = false) CommonsMultipartFile file,
			@RequestParam(defaultValue = "public") String model, HttpServletRequest request, ImageFile ff)
			throws IllegalStateException, IOException {

		String lastFilePth = null;
		// 判断是否有文件
		if (file != null && !file.isEmpty()) {
			// 获取文件的原始名称
			String oldName = file.getOriginalFilename();
			// 获取文件大小
			// Long fileSize = file.getSize();
			// 获取文件的原始流
			// f.getInputStream()
			// 获取文件的类型
			// System.out.println(file.getContentType());

			// 组装文件存储路径
			String filePath = rootPath + model + File.separator;

			// 创建目录
			File f = new File(filePath);
			if (!f.exists()) {
				f.mkdirs();
			}
			// 生成一个新的不会重复的文件名
			// 1.获取后缀
			String suffix = FilenameUtils.getExtension(file.getOriginalFilename());
			// 2.生成新的文件名
			String newFileName = UUID.randomUUID().toString() + "." + suffix;
			// 把上传的文件存储指定位置
			file.transferTo(new File(f, newFileName));// 文件上传操作
			String FilePath = rootPath + model + File.separator + newFileName;

			ff.setFilePath(FilePath.replace("\\", "/"));
			fileDao.save(ff);
			int fileId = ff.getFileId();
			System.out.println(fileId);
			fileIdMap.put("fileId", fileId);
		} else {
			System.out.println("上传失败！！");
		}
		return fileIdMap;
	}

	// 文件下载,表示/upload后面接的任何路径都会进入到这里
	/*
	 * @RequestMapping("/**") public void view(HttpServletResponse response,
	 * HttpServletRequest request, ImageFile ff) throws Exception { String filePath
	 * = request.getServletPath().replaceFirst("/upload/", ""); File file = new
	 * File(rootPath, filePath);
	 * 
	 * if (file.exists()) { List<ImageFile> list =
	 * fileDao.findByFilePath(request.getServletPath()); String fileName =
	 * list.get(0).getFileName();
	 * 
	 * // 设置下载文件的名称,如果想直接在想查看就注释掉，因为要是文件原名才能下载，不然就只能在浏览器直接浏览无法下载
	 * response.setHeader("content-disposition", "attachment;filename=" +
	 * URLEncoder.encode(fileName, "UTF-8"));
	 * 
	 * // 把文件输出到浏览器 OutputStream os = response.getOutputStream(); FileInputStream fs
	 * = new FileInputStream(file); byte[] b = new byte[1024]; int len = 0; while
	 * ((len = fs.read(b)) > 0) { os.write(b, 0, len); } fs.close(); os.flush(); }
	 * else { response.sendError(404); } }
	 */

	// 验证商品是否存在的ajax
	@ResponseBody
	@RequestMapping("/checkBackitemExist")
	public Map<String, Object> checkBackitemExist(
			@Param(value = "cid") String cid, @Param(value = "pid") String pid, @Param(value = "mid") String mid

	) {

		BackItem backitem = new BackItem();
		backitem.setCid(Integer.parseInt(cid));
		backitem.setPid(Integer.parseInt(pid));
		backitem.setMid(Integer.parseInt(mid));
		boolean flag = backItemService.checkExist(backitem);

		// 如果flag为true 说明数据库存在商品
		System.out.println(flag);
		// 返回false 禁止表单的提交
		Map<String, Object> map = new HashMap<>();
		map.put("rs", flag);
		return map;
	}

	// 跳转商品修改页面
	@RequestMapping("/backitemEdit")
	public String backitemEdit(Integer id, HttpServletRequest req) {
		
		BackItem backitem = backItemService.getOneById(id);
		// 获取回收商品图片信息的字符串
		String img_url = backitem.getImg_url();

	System.out.println("img"+img_url);
		// 判断评估商品是否有为空或者=""
		List<Integer> ids = new ArrayList<>();
		List<ImageFile> imageFiles = new ArrayList<>();
		if (img_url != null && img_url != "") {
			String str[] = img_url.split(";");
			// 此处从一开始
			for (int i = 1; i < str.length; i++) {

				ids.add(Integer.parseInt(str[i]));
			}	

		}
		if(ids.size()>0) {
			
			imageFiles = fileDao.getImageByid(ids);
		}

		req.setAttribute("imageFiles", imageFiles);

		req.setAttribute("backitem", backitem);
		
		List<BackAssessment> Values = backAssessmentService.selectBackAssessment(id);
		req.setAttribute("assessmentlist",Values);

		return "backitem_edit";
	}

	// 删除图片的ajax请求
	@ResponseBody
	@RequestMapping("updateImage")
	public Map<String, Object> updateImage(@RequestParam(value = "fileIds[]") String[] fileIds,
			@RequestParam(value = "id") int id) {
		Map<String, Object> map = new HashMap<>();

		String imageurl = "";
		System.out.println(id);
		BackItem backitem = backItemService.getOneById(id);

		String img_url = backitem.getImg_url();
		String[] imgs = img_url.split(";");

		// 将数据库中的数组元素对象放入list集合中
		List<String> list = new ArrayList<>();
		for (int i = 1; i < imgs.length; i++) {
			list.add(imgs[i]);
		}

		for (int i = 0; i < fileIds.length; i++) {
			// 判断如果商品对应img_url中存在前端传入的id 将从list集合中删除

			for (int j = 0; j < list.size(); j++) {

				if ((list.get(j)).equals((fileIds[i]))) {
					//删除原有的图片
					fileDao.deleteOneImage(Integer.parseInt(list.get(j)));
					list.remove(j);
				}
			}
		}
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				imageurl = ";" + imageurl + list.get(i);

			}
		} else {
			imageurl = "";
		}
		// 更新商品的图片信息
        
		//删除原有的图片
		
		
		backitem.setImg_url(imageurl);
		System.out.println(backitem);
		backItemService.updateBackItemImage(backitem);
		map.put("imageurl", imageurl);
		return map;
	}

	// 更新商品信息
	@RequestMapping("/updateBackitem")
	public String updateBackitem(BackItem backitem) {
		backitem.setUpdatetime(new Date());
		backItemService.updateBackItem(backitem);
		return "redirect:content-1_1";
	}

	// 获取商品详情
	@RequestMapping("/getBackinfo")
	public String getBackinfo(HttpServletRequest req, Integer id) {
		req.setAttribute("backiteminfo", backItemService.getOneById(id));
		List<ImageFile> filelist = new ArrayList<>();
		// 获取商品的第一张主图
		String filePath = backItemService.getOneById(id).getImg_url();
		// 判断商品是否有图片有就去查询图片
		List<Integer> list = new ArrayList<>();
		if (filePath != null && filePath != "") {

			String[] fileid = (filePath.split(";"));
			
			// 注意此处从1开始
			for (int i = 1; i < fileid.length; i++) {

				list.add(Integer.parseInt(fileid[i]));
			}
			

		}
		if (list.size() > 0) {
			filelist = fileDao.getImageByid(list);
			req.setAttribute("filepath", filelist.get(0));
		}
		
		return "backitem_info";
	}

	// 改变商品状=态的ajax
	@ResponseBody
	@RequestMapping("/changeBackStatus")
	public boolean changeBackStatus(Integer id) {
		System.out.println("---改变商品状态----");
		BackItem backItem = backItemService.getOneById(id);
		int backstatus = 0;
            System.out.println(backItem);
		// 如果数据库查到的状态为0 则将状态设为1 否则 为0
		if (backItem.getBackstatus() == 0) {
			backstatus = 1;
		}
		backItem.setUpdatetime(new Date());
		backItem.setBackstatus(backstatus);
		backItemService.changeBackStatus(backItem);
		
		return true;

	}
    
	@ResponseBody
	@RequestMapping("/backImgupdate")
	public Map<String, Object> imgupdate(@RequestParam(defaultValue = "public") String model,Integer fileid,
			//需要修改的商品的filepath
			@RequestParam(value="file") MultipartFile file) throws IllegalStateException, IOException {
		 
		Map<String, Object> map=new HashMap<>();
		  fileDao.deleteOneImage(fileid);
	          System.out.println(file);
			   if (file != null && !file.isEmpty()) {
					// 获取文件的原始名称
					String oldName = file.getOriginalFilename();
					// 获取文件大小
					// Long fileSize = file.getSize();
					// 获取文件的原始流
					// f.getInputStream()
					// 获取文件的类型
					// System.out.println(file.getContentType());

					// 组装文件存储路径
					String filePath = rootPath + model + File.separator;

					// 创建目录
					File f = new File(filePath);
					if (!f.exists()) {
						f.mkdirs();
					}
					// 生成一个新的不会重复的文件名
					// 1.获取后缀
					String suffix = FilenameUtils.getExtension(file.getOriginalFilename());
					// 2.生成新的文件名
					String newFileName = UUID.randomUUID().toString() + "." + suffix;
					// 把上传的文件存储指定位置
					file.transferTo(new File(f, newFileName));// 文件上传操作
					String FilePath = rootPath + model + File.separator + newFileName;
                    ImageFile ff = new ImageFile();
					ff.setFilePath(FilePath.replace("\\", "/"));
					fileDao.save(ff);
					int fileId = ff.getFileId();
					System.out.println(fileId);
					
					fileIdMap.put("ff", ff);
				
				} else {
					
					
					System.out.println("上传失败！！");
				
				
				}
				
			   return fileIdMap;
			   }
	
	
	
	
	@ResponseBody
	@RequestMapping("/deleteImage")
	public Map<String, Object> deleteImage(String fileId,Integer id){
		
		Map<String, Object> map = new HashMap<>();
		
		String imageurl="";
		
		
		String img_url = backItemService.getOneById(id).getImg_url();
		// 删除图片
		fileDao.deleteOneImage(Integer.parseInt(fileId));
		
		 List<String> list = new ArrayList<>();
		String img[]=null;
		if(img_url!=null&&img_url!="") {
			
			 img =img_url.split(";");
		}
		
		for(int i=1;i<img.length;i++) {
			
			list.add(img[i]);
		
		}
		
		for(int i=0;i<list.size();i++) {
			
			if(list.get(i).equals(fileId)) {
				
				list.remove(list.get(i));
			}else {
				
				if(imageurl=="") {
					
					imageurl = ";" +list.get(i);	
				}else {
					
					imageurl=imageurl+";"+list.get(i);
				}
				
				
			}
			
		}
		
		System.out.println("----无敌的存在"+imageurl);
		
		map.put("imageurl", imageurl);
		
		return map;
	}
	
	//修改页面加载时获取三级菜单数据
	@ResponseBody
	@RequestMapping("/getThreeUnit")
	public Map<String, Object> getThreeeUnit(Integer id,HttpServletRequest req){
		Map<String, Object> map = new HashMap<>();
		
         BackItem backitem = backItemService.getOneById(id);
		
		map.put("classifylist", classifyService.getAllClassify());

		List<Brand> brandlist = brandService.getAllBrand(backitem.getCid());
		// 再获取集合中第一个classify 对象的id
		map.put("brandlist", brandlist);

		map.put("modellist", modelService.getModelByid(backitem.getPid()));
		
		map.put("backitem", backitem);
		
		return map;
	}
	
	
	
	
	
	
	
}
